package com.dpwgc.pan.service;

import com.dpwgc.pan.mapper.NotepadMapper;
import com.dpwgc.pan.model.Notepad;
import com.dpwgc.pan.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NotepadService {

    @Autowired
    NotepadMapper notepadMapper;

    /**
     * 新建记事本
     * @param notepad
     * @return
     */
    public ResultUtil<Object> insertNotepadService(Notepad notepad){

        ResultUtil<Object> resultUtil = new ResultUtil<>();

        try{

            int i = notepadMapper.insert(notepad);

            resultUtil.setCode(200);
            resultUtil.setMsg("新建成功");
            resultUtil.setData(i);
            return resultUtil;

        }catch (Exception e){

            resultUtil.setCode(100);
            resultUtil.setMsg("新建失败");
            resultUtil.setData(e);
            return resultUtil;
        }

    }

    /**
     * 更新记事本
     * @param notepad
     * @return
     */
    public ResultUtil<Object> updateNotepadByUuidService(Notepad notepad){

        ResultUtil<Object> resultUtil = new ResultUtil<>();

        try{
            int i = notepadMapper.updateNotepadByUuid(notepad);

            resultUtil.setCode(200);
            resultUtil.setMsg("已保存");
            resultUtil.setData(i);
            return resultUtil;

        }catch (Exception e){

            resultUtil.setCode(100);
            resultUtil.setMsg("保存失败");
            resultUtil.setData(e);
            return resultUtil;
        }

    }

    /**
     * 删除记事本
     * @param notepadUuid
     * @return
     */
    public ResultUtil<Object> deleteNotepadByUuidService(String notepadUuid){

        ResultUtil<Object> resultUtil = new ResultUtil<>();

        notepadMapper.deleteNotepadByUuid(notepadUuid);

        try{
            int i = notepadMapper.deleteNotepadByUuid(notepadUuid);

            resultUtil.setCode(200);
            resultUtil.setMsg("删除成功");
            resultUtil.setData(i);
            return resultUtil;

        }catch (Exception e){

            resultUtil.setCode(100);
            resultUtil.setMsg("删除失败");
            resultUtil.setData(e);
            return resultUtil;
        }

    }

    /**
     * 获取该记事本详情
     * @param notepadUuid
     * @return
     */
    public ResultUtil<Object> getNotepadByUuidService(String notepadUuid){

        ResultUtil<Object> resultUtil = new ResultUtil<>();

        notepadMapper.getNotepadByUuid(notepadUuid);

        try{
            Notepad notepad = notepadMapper.getNotepadByUuid(notepadUuid);

            resultUtil.setCode(200);
            resultUtil.setMsg("操作成功");
            resultUtil.setData(notepad);
            return resultUtil;

        }catch (Exception e){

            resultUtil.setCode(100);
            resultUtil.setMsg("操作失败");
            resultUtil.setData(e);
            return resultUtil;
        }

    }

    /**
     * 获取用户记事本列表
     * @param userId
     * @return
     */
    public ResultUtil<Object> listNotepadByUserIdService(int userId){

        ResultUtil<Object> resultUtil = new ResultUtil<>();

        try{
            List<Notepad> notepads = notepadMapper.listNotepadByUserId(userId);

            resultUtil.setCode(200);
            resultUtil.setMsg("操作成功");
            resultUtil.setData(notepads);
            return resultUtil;

        }catch (Exception e){

            resultUtil.setCode(100);
            resultUtil.setMsg("操作失败");
            resultUtil.setData(e);
            return resultUtil;
        }

    }
}
